﻿//--------------------------------------------------[CutTitle]------------------------------------------------------------------------------------
/*
函数功能：
字符串剪切处理.。
参数：
str:源字符串，len截取长度，_pad补尾。
*/
function CutTitle(str, len, _pad) {
    var pad = _pad;
    if (pad == null) {
        pad = "...";
    }
    var padLen = pad.length;

    if ((str != null) && (str.length > len) && (len > padLen)) {
        var ss = str.replace(/\\n\\r/g, "");
        return str.substring(0, len - padLen) + pad;
    }
    return str;
}
//--------------------------------------------------[HtmlEncode]------------------------------------------------------------------------------------
/*
函数功能：
js 字符串Html编码。
参数：
text:源字符串，。
*/
function HtmlEncode(text, allowNull) {
    if (text == null) {
        return allowNull ? text : "";
    }
    return text.replace(/&/g, '&amp').replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
//--------------------------------------------------[GetDate]------------------------------------------------------------------------------------
/*
函数功能：
DateTime转换成 "年-月-日"字符串。
参数：
d: DateTime.
*/
function GetDate(d) {
    if (d == null) {
        return null;
    }
    return d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
}

/*
函数功能：
将yyyy-MM-dd格式的日期字符串换转成yyyy年MM月dd日。
*/
function GetChsDateString(d) {
    if (d == "" || d == null) return "";
    var ds = d.split('-');
    return ds[0] + "年" + parseInt(ds[1],10).toString() + "月" + parseInt(ds[2],10).toString() + "日";
}
//--------------------------------------------------[getLTWH]------------------------------------------------------------------------------------
/*
函数功能：
兼容IE和FF返回目标对象包含边框的left、top、width、height值。其中left、top是相对于document.body的坐标。
需要参数1个：
[DOM]o=[DOM]                        要取值的对象。
*/
function getLTWH(o) {
    function getCurrentStyle(style) {
        var number = parseInt(o.currentStyle[style]);
        return isNaN(number) ? 0 : number;
    }
    function getComputedStyle(style) {
        return parseInt(document.defaultView.getComputedStyle(o, null).getPropertyValue(style));
    }
    var oLTWH =
        {
            "left": o.offsetLeft,
            "top": o.offsetTop,
            "width": o.offsetWidth,
            "height": o.offsetHeight
        };
    var ie = !!(window.attachEvent && !window.opera);
    while (true) {
        o = o.offsetParent;
        if (o == (document.body && null)) break;
        oLTWH.left += o.offsetLeft;
        oLTWH.top += o.offsetTop;
        if (ie) {
            oLTWH.left += getCurrentStyle("borderLeftWidth");
            oLTWH.top += getCurrentStyle("borderTopWidth");
        }
        else {
            oLTWH.left += getComputedStyle("border-left-width");
            oLTWH.top += getComputedStyle("border-top-width");
        }
    }
    return oLTWH;
}
//--------------------------------------------------[Function.prototype.bind ]------------------------------------------------------------------------------------
/*
函数功能：
Function扩展，对象绑定
需要参数1个：
obj：要绑定到的对象。
*/
Function.prototype.bind = function(obj) {
    var method = this,
    temp = function() {
        return method.apply(obj, arguments);
    };
    return temp;
}

//--------------------------------------------------[GetPageSize]------------------------------------------------------------------------------------
/*
函数功能：
取 window、Page的宽、高。
*/
function GetPageSize() {
    var scrW, scrH;
    if (window.innerHeight && window.scrollMaxY) {   // Mozilla    
        scrW = window.innerWidth + window.scrollMaxX;
        scrH = window.innerHeight + window.scrollMaxY;
    }
    else {    // IE
        scrW = (document.body.scrollWidth > document.body.offsetWidth) ? document.body.scrollWidth : document.body.offsetWidth;
        scrH = (document.body.scrollHeight > document.body.offsetHeight) ? document.body.scrollHeight : document.body.offsetHeight;
    }

    //窗口尺寸
    var winW = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
    var winH = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;

    // page尺寸取大
    var pageW = (scrW < winW) ? winW : scrW;
    var pageH = (scrH < winH) ? winH : scrH;
    return { PageW: pageW, PageH: pageH, WinW: winW, WinH: winH };
}

//--------------------------------------------------[addEventHandler ]------------------------------------------------------------------------------------
/*
函数功能：
添加事件监听
需要参数：
target 载体
type 事件类型
func 事件函数
*/
function addEventHandler(target, type, func) {
    if (target.addEventListener) {
        target.addEventListener(type, func, false);
    }
    else if (target.attachEvent) {
        target.attachEvent("on" + type, func);
    }
    else {
        target["on" + type] = func;
    }
}
//--------------------------------------------------[removeEventHandler ]------------------------------------------------------------------------------------
/*
函数功能：
移除事件监听
需要参数：
target 载体
type 事件类型
func 事件函数
*/
function removeEventHandler(target, type, func) {
    if (target.removeEventListener) {
        target.removeEventListener(type, func, false);
    }
    else if (target.detachEvent) {
        target.detachEvent("on" + type, func);
    }
    else {
        delete target["on" + type];
    }
}

/**************************************************
* dom-drag.js
* 09.25.2001
* www.youngpup.net
**************************************************
* 10.28.2001 - fixed minor bug where events
* sometimes fired off the handle, not the root.
**************************************************/

var Drag = {

    obj: null,

    init: function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper) {
        o.onmousedown = Drag.start;

        o.hmode = bSwapHorzRef ? false : true;
        o.vmode = bSwapVertRef ? false : true;

        o.root = oRoot && oRoot != null ? oRoot : o;

        if (o.hmode && isNaN(parseInt(o.root.style.left))) o.root.style.left = "0px";
        if (o.vmode && isNaN(parseInt(o.root.style.top))) o.root.style.top = "0px";
        if (!o.hmode && isNaN(parseInt(o.root.style.right))) o.root.style.right = "0px";
        if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

        o.minX = typeof minX != 'undefined' ? minX : null;
        o.minY = typeof minY != 'undefined' ? minY : null;
        o.maxX = typeof maxX != 'undefined' ? maxX : null;
        o.maxY = typeof maxY != 'undefined' ? maxY : null;

        o.xMapper = fXMapper ? fXMapper : null;
        o.yMapper = fYMapper ? fYMapper : null;

        o.root.onDragStart = new Function();
        o.root.onDragEnd = new Function();
        o.root.onDrag = new Function();
    },

    start: function(e) {
        var o = Drag.obj = this;
        e = Drag.fixE(e);
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        o.root.onDragStart(x, y);

        o.lastMouseX = e.clientX;
        o.lastMouseY = e.clientY;

        if (o.hmode) {
            if (o.minX != null) o.minMouseX = e.clientX - x + o.minX;
            if (o.maxX != null) o.maxMouseX = o.minMouseX + o.maxX - o.minX;
        } else {
            if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
            if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
        }

        if (o.vmode) {
            if (o.minY != null) o.minMouseY = e.clientY - y + o.minY;
            if (o.maxY != null) o.maxMouseY = o.minMouseY + o.maxY - o.minY;
        } else {
            if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
            if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
        }

        document.onmousemove = Drag.drag;
        document.onmouseup = Drag.end;

        return false;
    },

    drag: function(e) {
        e = Drag.fixE(e);
        var o = Drag.obj;

        var ey = e.clientY;
        var ex = e.clientX;
        var y = parseInt(o.vmode ? o.root.style.top : o.root.style.bottom);
        var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right);
        var nx, ny;

        if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
        if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
        if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
        if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

        nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
        ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

        if (o.xMapper) nx = o.xMapper(y)
        else if (o.yMapper) ny = o.yMapper(x)

        Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
        Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
        Drag.obj.lastMouseX = ex;
        Drag.obj.lastMouseY = ey;

        Drag.obj.root.onDrag(nx, ny);
        return false;
    },

    end: function() {
        document.onmousemove = null;
        document.onmouseup = null;
        Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
                                    parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
        Drag.obj = null;
    },

    fixE: function(e) {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
};


function QueryString(item) {
    var sValue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"))
    return sValue ? sValue[1] : sValue
}

